// pages/user/obtain-goods/obtain-goods.js
import Dialog from '../../../miniprogram/miniprogram_npm/vant-weapp/dialog/dialog'
import Toast from '../../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'

let app = getApp();
const moment = require('../../../utils/moment.min.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: '',
    nowDate: new Date().getTime(),
    isDominate: false,
    dominateActions: [//拔取到
      {name: '团购', isKuaidi: 0},
      {name: '快递直发', isKuaidi: 1}
    ],
    isKind: false,
    kindActions: [
      {name: '普通商品', type: 0},
      {name: '拼团商品', type: 1}
    ],
    isStaDate: false,
    isEndDate: false,
    isLiansuoid: false,
    liansuoidColumns: [],
    isHe: false,
    heColumns: [],

    goods: '',

    isKuaidi: 0,// 0团购 1快递直发
    kind: 0,// 0普通商品 1拼团商品
    reqDominate: '团购',//拔取到
    reqKind: '普通商品',//商品类型
    reqKindNumber: '',//拼团人数
    reqUsercommission: '',//推荐人佣金
    reqSuperusercommission: '',//推荐人上级佣金
    reqLiansuoid: '',
    reqLiansuoidId: '',
    reqGoodsName: '',//商品名称
    reqMarketprice: '',//现价
    reqProductprice: '',//原价
    reqCostprice: '',//成本价
    reqTotal: '',//库存
    reqDisplayorder: '',//排序
    reqStaData: '',//上架时间
    reqEndData: '',//下架时间
    reqSupplier: '',//供应商
    reqSupplierId: '',//供应商id
    reqHeadcommission: '',//门店佣金
    reqSuperiorcommission: '',//门店推荐人佣金

  },

  //拔取
  submitClick(){
    let _this = this;
    //用户身份不是门店
    if(_this.data.user.isteam != 3){
      Dialog.confirm({
        title: '门店申请',
        message: '需要门店才能拔取',
        confirmButtonText: '门店申请'
      }).then(()=>{
        wx.navigateTo({
          url: '/sub/user/shop-ask/shop-ask'
        })
      }).catch(()=>{})
      return;
    }

    if(_this.data.kind == '1' && !_this.data.reqKindNumber){
      Toast('请填写拼团人数');return;
    }
    if (!_this.data.reqGoodsName) {
      Toast('请填写商品名称');return;
    }
    if (!_this.data.reqMarketprice) {
      Toast('请填写现价');return;
    }
    if (!_this.data.reqProductprice) {
      Toast('请填写原价');return;
    }
    if (!_this.data.reqHeadcommission) {
      Toast('请填写门店佣金');return;
    }
    if (!_this.data.reqSuperiorcommission) {
      Toast('请填写门店推荐佣金');return;
    }
    if (!_this.data.reqTotal) {
      Toast('请填写库存');return;
    }
    if (!_this.data.reqDisplayorder) {
      Toast('请填写排序');return;
    }
    if (_this.data.isKuaidi == 1 && !_this.data.reqStaData) {
      Toast('请选择上架时间'); return;
    }
    if (_this.data.isKuaidi == 1 && !_this.data.reqEndData) {
      Toast('请选择下架时间'); return;
    }

    if (_this.data.reqSupplier && !_this.data.reqCostprice) {
      Toast('请填写成本价'); return;
    }
    if(_this.data.reqCostprice > 0 && !_this.data.reqSupplier){
      Toast('请选择供应商'); return;
    }

    app.requestUrl({
      method: 'POST',
      url: '/wxapi/product/saveProductCentre',
      data: {
        centreIds: [_this.data.user.centreId],
        ProductCentre: [{
          id: _this.data.goods.id,
          title: _this.data.reqGoodsName,
          marketprice: _this.data.reqMarketprice,
          productprice: _this.data.reqProductprice,
          costprice: _this.data.reqCostprice,
          total: _this.data.reqTotal,
          displayorder: _this.data.reqDisplayorder,
          headcommission: _this.data.reqHeadcommission,
          superiorcommission: _this.data.reqSuperiorcommission,
          supplierId: _this.data.reqSupplierId,
          isKuaidi: _this.data.isKuaidi,
          isSpellgroup: _this.data.kind == '1' ? _this.data.kind : '',//1 拼团商品
          spellgroupnum: _this.data.reqKindNumber,
          usercommission: _this.data.reqUsercommission,
          superusercommission: _this.data.reqSuperusercommission,
          liansuoid: _this.data.reqLiansuoidId,
          upStartTime: _this.data.reqStaData,
          upEndTime: _this.data.reqEndData,
        }]
      },
      header: {
        'Content-Type': 'application/json;charset=UTF-8',
        'Authorization': wx.getStorageSync('authToken') || 'empty-token'
      },
      load: 1
    }).then(res =>{
      Dialog.alert({
        title: '提示',
        message: '拔取成功',
      }).then(()=>{
        wx.navigateBack({
          detail: 1
        })
      }).catch(()=>{})
    })
  },

  //门店接口
  liansuoReq(){
    let _this = this;
    app.requestUrl({
      method: 'POST',
      url: '/wxapi/mem/getTeamByCentreId',
      data: {centreId: app.globalData.loginCentreId},
      load: 1
    }).then(res =>{
      _this.setData({
        liansuoidColumns: res.data.data
      })
    })
  },

  //供应商接口
  supplierReq() {
    let _this = this;
    app.requestUrl({
      method: 'POST',
      url: '/wxapi/getListSupplys',
      data: {centreId: app.globalData.loginCentreId},
      load: 1
    }).then(res =>{
      _this.setData({
        heColumns: res.data.data
      })
    })
  },

  //控制"拔取到"弹框
  dominateControl() {
    let _this = this;
    _this.setData({
      isDominate: !_this.data.isDominate
    })
  },
  dominateSelect(event){
    this.setData({
      reqDominate: event.detail.name,
      isKuaidi: event.detail.isKuaidi
    })
  },
  //控制"商品类型"弹框
  kindControl() {
    let _this = this;
    _this.setData({
      isKind: !_this.data.isKind
    })
  },
  kindSelect(event){
    this.setData({
      reqKind: event.detail.name,
      kind: event.detail.type
    })
  },
  //控制"上架时间"弹框
  staDateControl(){
    let _this = this;
    _this.setData({
      isStaDate: !_this.data.isStaDate
    })
  },
  staDateConfirm(event){
    this.setData({
      reqStaData: moment(event.detail).format('YYYY-MM-DD hh:mm:ss'),
    })
    this.staDateControl();
  },
  //控制"下架时间"弹框
  endDateControl(){
    let _this = this;
    _this.setData({
      isEndDate: !_this.data.isEndDate
    })
  },
  endDateConfirm(event){
    this.setData({
      reqEndData: moment(event.detail).format('YYYY-MM-DD hh:mm:ss'),
    })
    this.endDateControl();
  },
  //控制"门店"弹框
  liansuoidControl(){
    let _this = this;
    _this.setData({
      isLiansuoid: !_this.data.isLiansuoid
    })
  },
  liansuoidConfirm(event){
    this.setData({
      reqLiansuoid: event.detail.value.nickname,
      reqLiansuoidId: event.detail.value.id
    })
    this.liansuoidControl();
  },
  //控制"供应商"弹框
  isHeControl(){
    let _this = this;
    _this.setData({
      isHe: !_this.data.isHe
    })
  },
  heConfirm(event){
    this.setData({
      reqSupplier: event.detail.value.name,
      reqSupplierId: event.detail.value.id
    })
    this.isHeControl();
  },

  usercommissionBlur(event){
    this.setData({reqUsercommission: event.detail.value})
  },
  superusercommissionBlur(event){
    this.setData({reqSuperusercommission: event.detail.value})
  },
  kindNumberBlur(event){
    this.setData({reqKindNumber: event.detail.value})
  },
  goodsNameBlur(event){
    this.setData({reqGoodsName: event.detail.value})
  },
  marketpriceBlur(event){
    let _this = this;
    let value = event.detail.value;
    //门店佣金
    if (_this.data.goods.headcommission > 0 && _this.data.goods.headcommission != '')
      _this.data.reqHeadcommission = parseFloat((_this.data.goods.headcommission / 100)).toFixed(2) * value;
    //门店推荐人佣金
    if (_this.data.goods.superiorcommission > 0 && _this.data.goods.superiorcommission != '')
      _this.data.reqSuperiorcommission = parseFloat((_this.data.goods.superiorcommission / 100)).toFixed(2) * value;
    this.setData({
      reqMarketprice: value,
      reqHeadcommission: _this.data.reqHeadcommission,
      reqSuperiorcommission: _this.data.reqSuperiorcommission,
    })
  },
  productpriceBlur(event){
    this.setData({reqProductprice: event.detail.value})
  },
  costpriceBlur(event){
    this.setData({reqCostprice: event.detail.value})
  },
  headcommissionBlur(event){
    this.setData({reqHeadcommission: event.detail.value})
  },
  superiorcommissionBlur(event){
    this.setData({reqSuperiorcommission: event.detail.value})
  },
  totalBlur(event){
    this.setData({reqTotal: event.detail.value})
  },
  displayorderBlur(event){
    this.setData({reqDisplayorder: event.detail.value})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (event) {
    this.setData({
      user: app.getUser(),
      goods: JSON.parse(event.goods),
      reqGoodsName: JSON.parse(event.goods).title
    })
    this.liansuoReq();
    this.supplierReq();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
